package com.larry.mr.mapjoin;

import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName hadoop
 * @Author larry
 * @Date 17:01 2021/11/16
 * @Description TODO    mapjoin
 **/
public class MapJoinMapper extends Mapper<LongWritable, Text,Text, NullWritable> {

    private Map<String,String> map = new HashMap<>();
    private Text outK = new Text();

    /**
     * 处理缓存文件
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        URI[] files = context.getCacheFiles();
        URI file = files[0];
        FileSystem fs = FileSystem.get(context.getConfiguration());
        FSDataInputStream fsin = fs.open(new Path(file));

        //读数据
        BufferedReader br = new BufferedReader(new InputStreamReader(fsin,"UTF-8"));
        String line;
        while ((line = br.readLine()) != null){
            String[] split = line.split("\t");
            map.put(split[0],split[1]);
        }

        //关闭资源
        IOUtils.closeStream(br);
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] split = value.toString().split("\t");
        String pname = map.get(split[1]);

        //输出
        String res = split[0] + "\t" + pname + "\t" + split[2];
        outK.set(res);
        context.write(outK,NullWritable.get());
    }
}
